package com.regent.central.bill.web.mrp.arrive;

import com.regent.central.bill.common.BillPurchaseArriveStatus;
import com.regent.central.bill.common.DataChangeType;
import com.regent.central.bill.repository.mrp.dto.BillMaterialArriveAddDTO;
import com.regent.central.bill.repository.mrp.dto.MaterialArriveDTO;
import com.regent.central.bill.repository.mrp.dto.arrive.*;
import com.regent.central.bill.service.mrp.BillMaterialArriveService;
import com.regent.central.bill.service.mrp.BillMaterialPurchaseSkuService;
import com.regent.central.bill.service.mrp.exception.MrpServiceException;
import com.regent.central.bill.web.mrp.*;
import com.regent.central.infrastructure.web.BasicResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 新建采购单Controller
 *
 * @author 钟宝林
 * @date 2018-06-16 14:51
 **/
@RestController
@RequestMapping(value = "/bill/material/arrive", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class BillMaterialArriveController {

    @Resource
    private BillMaterialArriveService billMaterialArriveService;

    @Resource
    private BillMaterialPurchaseSkuService billMaterialPurchaseSkuService;

    @PostMapping("/add")
    public BillMaterialArriveCreateResponse create(@RequestBody BillMaterialArriveAddDTO arriveAddDTO) {
        BillMaterialArriveCreateResponse response = new BillMaterialArriveCreateResponse();
        BillMaterialArriveCreateResult result = billMaterialArriveService.create(arriveAddDTO);
        response.setResult(result);
        return response;
    }

    @GetMapping("/{billMaterialArriveId:\\w+}")
    public BillMaterialArriveDetailResponse detail(@PathVariable("billMaterialArriveId") String billMaterialArriveId) {
        BillMaterialArriveDetailResponse billMaterialArriveDetailResponse = new BillMaterialArriveDetailResponse();
        BillMaterialArriveDetailDTO billMaterialArriveDetailDTO = billMaterialArriveService.detail(billMaterialArriveId);
        billMaterialArriveDetailResponse.setDetail(billMaterialArriveDetailDTO);
        return billMaterialArriveDetailResponse;
    }

    @GetMapping("/findOrderNos/{unitId:\\w+}")
    public BillMaterialArriveOrderNoResponse findOrderNos(@PathVariable("unitId") String unitId) {
        BillMaterialArriveOrderNoResponse response = new BillMaterialArriveOrderNoResponse();
        if(StringUtils.isEmpty(unitId)){
            response.setCode(1);
            response.setMessage("unitId不能为空");
            return response;
        }
        List<String> orderNos = billMaterialArriveService.findOrderNosByUnitId(unitId);
        response.setOrderNos(orderNos);
        return response;
    }

    /**
     * 做验布和缩布前的查询
     */
    @GetMapping("/cs/material-sku/{billMaterialArriveId:\\w+}")
    public BillMaterialArriveMaterialSkuCSResponse csMaterialSku(@PathVariable("billMaterialArriveId") String billMaterialArriveId) {
        BillMaterialArriveMaterialSkuCSResponse response = new BillMaterialArriveMaterialSkuCSResponse();
        List<MaterialSkuCSDTO> materialSkuCSDTOList = billMaterialArriveService.csMaterialSku(billMaterialArriveId);
        response.setMaterialSkuCSDTOList(materialSkuCSDTOList);
        return response;
    }

    @PostMapping("/check")
    public BasicResponse check(@RequestBody ArriveCheckDTO arriveCheckDTO) {
        BasicResponse response = new BasicResponse();
        try {
            billMaterialArriveService.check(arriveCheckDTO);
        } catch (MrpServiceException e) {
            e.printStackTrace();
            response.setCode(400);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @PostMapping("/shrunk")
    public ArriveShrunkResponse shrunk(@RequestBody ArriveShrunkDTO arriveShrunkDTO) {
        ArriveShrunkResponse response = new ArriveShrunkResponse();
        List<ArriveShrunkStockChangeResult> results = null;
        try {
            results = billMaterialArriveService.shrunk(arriveShrunkDTO);
        } catch (MrpServiceException e) {
            e.printStackTrace();
            response.setCode(400);
            response.setMessage(e.getMessage());
        }
        response.setResults(results);
        return response;
    }

    /**
     * 关联验布引起的库存变动记录
     */
    @PutMapping("/check/stock-change")
    public BasicResponse checkStockChange(@RequestBody ShrunkStockChangeDTO dto) {
        billMaterialArriveService.checkStockChange(dto.getStockChanges());
        return new BasicResponse();
    }

    /**
     * 关联缩布引起的库存变动记录
     */
    @PutMapping("/shrunk/stock-change")
    public BasicResponse shrunkStockChange(@RequestBody ShrunkStockChangeDTO dto) {
        billMaterialArriveService.shrunkStockChange(dto.getStockChanges());
        return new BasicResponse();
    }

    /**
     * 撤销验布
     */
    @GetMapping("/undo-check")
    public UndoCheckResponse undoCheck(@RequestParam("billMaterialArriveId") String billMaterialArriveId, @RequestParam("materialSkuId") String materialSkuId) {
        UndoCheckResponse response = new UndoCheckResponse();
        UndoCheckResult result = billMaterialArriveService.undoCheck(billMaterialArriveId, materialSkuId);
        response.setResult(result);
        return response;
    }

    /**
     * 撤销缩布
     */
    @GetMapping("/undo-shrunk")
    public UndoShrunkResponse undoShrunk(@RequestParam("billMaterialArriveId") String billMaterialArriveId, @RequestParam("materialSkuId") String materialSkuId) {
        UndoShrunkResponse response = new UndoShrunkResponse();
        UndoShrunkResult result = billMaterialArriveService.undoShrunk(billMaterialArriveId, materialSkuId);
        response.setResult(result);
        return response;
    }

    /**
     * 到料单分页查询列表
     */
    @GetMapping
    public BillMaterialArrivePageDTO queryBillArrive(BillMaterialArriveQueryDTO billMaterialArriveQueryDTO) {
        return billMaterialArriveService.listMaterialArrive(billMaterialArriveQueryDTO, billMaterialArriveQueryDTO.getPageNum(),
                billMaterialArriveQueryDTO.getPageSize());
    }

    /**
     * 到料单导出Excel
     */
    @PostMapping("/arriveExcel")
    public BillMaterialArriveExcelResponse arriveExcel(@RequestBody BillMaterialArriveQueryDTO billMaterialArriveQueryDTO) {
        BillMaterialArriveExcelResponse response = new BillMaterialArriveExcelResponse();
        try{
            List<MaterialArriveDTO> list = billMaterialArriveService.arriveExcel(billMaterialArriveQueryDTO);
            response.setList(list);
        }catch (Exception e){
            response.setCode(1);
            response.setMessage("查询失败");
        }
        return response;
    }

    @PutMapping("/undo/{billMaterialArriveId:\\w+}")
    public UndoBillArriveResponse undo(@PathVariable("billMaterialArriveId") String billMaterialArriveId) {
        UndoBillArriveResponse response = new UndoBillArriveResponse();
        BillArriveUndoResult result = billMaterialArriveService.undo(billMaterialArriveId);
        response.setResult(result);
        return response;
    }

    /**
     * 关联库存变动记录
     *
     * @param billMaterialArriveId 到料单ID
     * @param stockChangeId        库存变动记录ID
     */
    @GetMapping("/relation-stock-change")
    public BasicResponse relationStockChange(@RequestParam("billMaterialArriveId") String billMaterialArriveId,
                                             @RequestParam("stockChangeId") String stockChangeId) {
        BasicResponse response = new BasicResponse();
        if (StringUtils.isBlank(billMaterialArriveId) || StringUtils.isBlank(stockChangeId)) {
            response.setCode(400);
            response.setMessage("参数不全");
            return response;
        }

        billMaterialArriveService.relationStockChange(billMaterialArriveId, stockChangeId);
        return response;
    }

    @GetMapping("/test")
    public int test() {
        return billMaterialPurchaseSkuService.setArriveStatus(BillPurchaseArriveStatus.COMPLETED,
                "420c29e61fd6491ebe6302b6eb523550",
                "12c3a351ee8044bcae6a1093ced32829", "68e89bde146044aebb69b503341c83ea", DataChangeType.USER_OPERATE);
    }


}
